package com.test39_集合应用.service.impl;

import com.test39_集合应用.service.IStudentService;
import com.test39_集合应用.util.MathUtil;
import com.test39_集合应用.vo.Student;

import java.util.*;

public class StudentServiceImpl implements IStudentService {
    private List<Student> allStudents = new LinkedList<>();

    /** 保存数据到集合之中 */
    @Override
    public void add(Student student) {
        this.allStudents.add(student);
    }

    /** 得到每位学生的总成绩 */
    @Override
    public Map<String, Double> getAllScore() {
        Map<String, Double> result = new HashMap<>();
        for (Student student : allStudents) {
            result.put(student.getName(), MathUtil.round((student.getMath() + student.getProgram() + student.getEnglish()), 2));
        }
        return result;
    }

    /** 得到每门课程的平均成绩 */
    @Override
    public Map<String, Double> getAvgScore() {
        Map<String, Double> result = new LinkedHashMap<>();
        result.put("math", 0.0);
        result.put("program", 0.0);
        result.put("english", 0.0);
        for (Student student : allStudents) {
            result.put("math", result.get("math") + student.getMath());
            result.put("program", result.get("program") + student.getProgram());
            result.put("english", result.get("english") + student.getEnglish());
        }
        result.put("math", MathUtil.round((result.get("math") / allStudents.size()), 2));
        result.put("program", MathUtil.round((result.get("program") / allStudents.size()), 2));
        result.put("english", MathUtil.round((result.get("english") / allStudents.size()), 2));
        return result;
    }

    /** 获取全部的学生信息 */
    @Override
    public List<Student> findAll() {
        return this.allStudents;
    }
}
